<template>
  <el-dialog
    :close-on-click-modal="false"
    :visible.sync="dialogFormVisible"
    :before-close="close"
    width="800px"
    :top="'10vh'"
  >
    <el-form ref="form" :model="form" size="small" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item
            label="方案名称"
            prop="title"
            :rules="[{ required: true, message: '请输入方案名称', trigger: 'blur' }]"
            style="width: 100%"
            maxlength="10"
            show-word-limit
          >
            <el-input
              v-model="form.title"
              placeholder="请输入方案名称"
              maxlength="10"
              show-word-limit
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="开始时间"
            prop="kssj"
            :rules="[{ required: true, message: '请选择开始时间', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-date-picker
              v-model="form.kssj"
              type="datetime"
              format="yyyy年MM月dd日HH:mm"
              value-format="yyyyMMddHHmm"
              placeholder="请选择开始时间"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="结束时间"
            prop="jssj"
            :rules="[{ required: true, message: '请选择结束时间', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-date-picker
              v-model="form.jssj"
              type="datetime"
              format="yyyy年MM月dd日HH:mm"
              value-format="yyyyMMddHHmm"
              placeholder="请选择结束时间"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="请选择主题"
            :prop="'theme.0.id'"
            :rules="[{ required: true, message: '请选择主题', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-select v-model="form.theme[0].id" clearable placeholder="请选择主题" @change="changeSelect">
              <el-option
                v-for="item in themes"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="状态"
            prop="zt"
            :rules="[{ required: true, message: '请选择状态', trigger: 'change' }]"
          >
            <el-radio-group v-model="form.zt">
              <el-radio
                v-for="item in status"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="close">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { queryAllTheme } from '@/api/system/theme/index'
import { saveEntity, updateEntity } from '@/api/system/theme/plan'
export default {
  name: 'PlanEntity',
  data() {
    return {
      dialogFormVisible: false,
      loading: false,
      themes: [],
      status: [{ id: 0, value: false, label: '禁用' },
        { id: 1, value: true, label: '启用' }],
      form: {
        id: null,
        type: 1,
        zt: true,
        kssj: '',
        jssj: '',
        title: '',
        theme: [
          {
            id: null,
            kssj: '',
            jssj: '',
            title: '',
            backGroundColor: '',
            fontColor: ''
          }
        ]
      }
    }
  },
  created() {
    this.initAllTheme()
  },
  methods: {
    init(data) {
      if (undefined !== data && undefined !== data.id && data.id !== null) {
        this.form = data
      }
      this.dialogFormVisible = true
    },
    initAllTheme() {
      queryAllTheme().then(res => {
        const { data } = res
        this.themes = data.data
      }).catch(() => {
        this.themes = []
      })
    },
    save(formName) {
      this.loading = true
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.form.id !== null && undefined !== this.form.id) {
            updateEntity(this.form).then(res => {
              this.$message.success('更新成功！')
              this.close()
            }).catch(() => {
              this.loading = false
              this.$message.error('更新失败！')
            })
          } else {
            saveEntity(this.form).then(res => {
              this.$message.success('保存成功！')
              this.close()
              this.loading = false
            }).catch(() => {
              this.loading = false
              this.$message.error('保存失败！')
            })
          }
        } else {
          this.loading = false
          return false
        }
      })
    },
    close() {
      this.loading = false
      this.fileList = []
      Object.assign(this.$data.form, this.$options.data.call(this).form)
      this.$refs['form'].resetFields()
      this.dialogFormVisible = false
      this.$emit('fetch-data')
    },
    changeSelect(data) {
      if (undefined !== data && data !== null && this.themes !== null && this.themes.length > 0) {
        const newData = this.themes.filter(item => item.id === data)
        if (undefined !== newData && newData.length > 0) {
          this.form.theme[0].id = newData[0].id
          this.form.theme[0].backGroundColor = newData[0].backGroundColor
          this.form.theme[0].fontColor = newData[0].fontColor
        } else {
          this.form.theme[0].id = null
          this.form.theme[0].backGroundColor = ''
          this.form.theme[0].fontColor = ''
        }
      } else {
        this.form.theme[0].id = null
        this.form.theme[0].backGroundColor = ''
        this.form.theme[0].fontColor = ''
      }
    }
  }
}
</script>

<style scoped>

</style>
